pragma Ada_2012;
with Ada.Text_IO;
with MIDI; 
with MIDI.XMIDI, MIDI.XMIDI.Convert_To_Text, MIDI.XMIDI.Convert_To_Midi;
with Ada.Streams; use Ada.Streams;
with Ada.Streams.Stream_IO;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Directories;
procedure XMI2MID (Path : String) is
   Prefix : String := "GAME";

   procedure Convert (Item : Ada.Directories.Directory_Entry_Type)
   is
      use Ada.Directories;
      Filename : String := Base_Name(Simple_Name(Item));
      Name : String := Filename(Filename'First + Prefix'Length .. Filename'Last);
      Target_Name : String := "./" & Name & ".mid";
      Source_Name : String := Full_Name(Item); -- Path & Prefix & Name & ".XMI";
      F : Stream_IO.File_Type;
   begin
      if not Exists(Target_Name) then
         Stream_IO.Create(F, Name => Target_Name);
         MIDI.XMIDI.Convert_To_Midi(Source_Name, Stream_IO.Stream(F));
         Stream_IO.Close(F);
         Ada.Text_IO.Put_Line("Convert midi " & Name & " OK");
      end if;
   end Convert;
begin
   Ada.Directories.Search(
      Path, 
      Prefix & "*.XMI", 
      (Ada.Directories.Ordinary_File => True, others => False),
      Convert'Access);
end XMI2MID;
